package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.HouseUser;
import com.atguigu.service.HouseUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author lhh
 * @ClassName HouseUserController
 * @date 2022/9/19 14:47
 */

@Controller
@RequestMapping("houseUser")
public class HouseUserController {

    @Reference
    private HouseUserService houseUserService;

    @GetMapping("create")
    public String create(Model model, @RequestParam("houseId") Long houseId){
        model.addAttribute("houseId",houseId);
        return "houseUser/create";
    }

    @PostMapping("save")
    public String save(HouseUser houseUser){
        houseUserService.save(houseUser);
        return "common/successPage";
    }

    @GetMapping("edit/{id}")
    public String edit(Model model, @PathVariable Long id){
        HouseUser houseUser = houseUserService.getById(id);
        model.addAttribute("houseUser",houseUser);
        return "houseUser/edit";
    }

    @PostMapping("update")
    public String update(HouseUser houseUser){
        houseUserService.update(houseUser);
        return "common/successPage";
    }

    @GetMapping("/delete/{houseId}/{id}")
    public String remove(@PathVariable Long houseId, @PathVariable Long id){
        houseUserService.remove(id);
        return "redirect:/house/"+houseId;
    }
}
